package com.juyu.miliao.api.controller.whitepacket;

import com.juyu.miliao.api.domain.GiftSpend;
import com.juyu.miliao.api.domain.dto.DynamicGiftInfoDTO;
import com.juyu.miliao.api.domain.dto.WhitePacketDynamicInfoDTO;
import com.juyu.miliao.api.domain.vo.WhitePacketDynamicDetailInfoVo;
import com.juyu.miliao.api.domain.vo.WhitePacketDynamicInfoVo;
import com.juyu.miliao.api.service.GiftSpendService;
import com.juyu.miliao.api.service.whitepacket.WhitePacketDynamicInfoService;
import com.juyu.miliao.api.util.PageInfoUtil;
import com.juyu.miliao.api.validator.GiftSpendCheck;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 白包社区动态信息controller
 * @author xuanqicheng
 * @create 2019-21-09:43
 */
@RestController
@RequestMapping("/packet")
@CrossOrigin
public class WhitePacketDynamicInfoController {

    @Autowired
    private WhitePacketDynamicInfoService whitePacketDynamicInfoService;

    @Autowired
    private GiftSpendService giftSpendService;

    /**
     * 发布动态
     * @param token
     * @param whitePacketDynamicInfoDTO
     * @param bindingResult
     */
    @PostMapping("/")
    public void addDynamicInfo(Authentication token, @Validated WhitePacketDynamicInfoDTO whitePacketDynamicInfoDTO,
                               BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw  new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }
        Subject subject = (Subject) token.getPrincipal();

        long userId = subject.getId();

        whitePacketDynamicInfoDTO.setUserId(userId);
        whitePacketDynamicInfoService.addDynamciInfo(whitePacketDynamicInfoDTO);
    }


    /**
     * 动态点赞
     * @param token
     * @param dynamicId
     * @return
     */
    @PostMapping("/like")
    public Object addUserVideoLove(Authentication token,long  dynamicId) {
        Subject subject = (Subject) token.getPrincipal();
        Long userId = subject.getId();
        return whitePacketDynamicInfoService.clickDynamicLike(userId,dynamicId);
    }

    /**
     * 更改分享次数
     */
    @PostMapping("/share")
    public void setShare(Long dynamicId){
        whitePacketDynamicInfoService.setShareInfo(dynamicId);
    }

    @GetMapping("/share/{dynamicId}")
    public Object getShareByDynamicId(@PathVariable("dynamicId") Long dynamicId){

        if(dynamicId == null || dynamicId ==0){
            throw new ConsumeRuntimeException("动态不正确");
        }

        Map<String,Object> obj = whitePacketDynamicInfoService.getShareDynamicInfoByDynamicId(dynamicId);

        return obj;
    }

    @GetMapping("/share/textContent/{dynamicId}")
    public WhitePacketDynamicDetailInfoVo getShareTextByDynamicId(@PathVariable("dynamicId") Long dynamicId){
        return whitePacketDynamicInfoService.getShareTextDynamicInfoByDynamicId(dynamicId);
    }

    /**
     * 删除动态
     * @param dynamicId
     */
    @PostMapping("/delete")
    public void deleteDynamicInfo(long dynamicId){
        whitePacketDynamicInfoService.deleteDynamicInfo(dynamicId);
    }


    /**
     * 查询动态列表
     * @param token
     * @param appKey
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/{appKey}/{page}-{pageSize}")
    public List<WhitePacketDynamicInfoVo> getDynamicInfoList(Authentication token, @PathVariable("appKey") String appKey,
                                                             @PathVariable("page") Integer page,
                                                             @PathVariable("pageSize") Integer pageSize) {
        if(StringUtils.isEmpty(appKey)){
            throw  new ConsumeRuntimeException("appKey不能为空");
        }
        Subject subject = (Subject) token.getPrincipal();

        PageInfoUtil pageInfo = new PageInfoUtil(page, pageSize).invoke();

        page = pageInfo.getPage();

        pageSize = pageInfo.getPageSize();

        long userId = subject.getId();

        List<WhitePacketDynamicInfoVo> list = whitePacketDynamicInfoService.getDynamicInfoList(userId,appKey,page, pageSize);

        return list;
    }


    /**
     * 根据用户id查询动态
     * @param token
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/{appKey}/{userId}/{page}-{pageSize}")
    public List<WhitePacketDynamicInfoVo> getUserDynamicInfoList(Authentication token, @PathVariable("appKey") String appKey,
                                                      @PathVariable("userId") Long userId,
                                                      @PathVariable("page") Integer page,
                                                      @PathVariable("pageSize") Integer pageSize) {
        if(userId == null || userId ==0){
            throw new ConsumeRuntimeException("用户id不正确");
        }
        if(StringUtils.isEmpty(appKey)){
            throw  new ConsumeRuntimeException("appKey不能为空");
        }

        PageInfoUtil pageInfo = new PageInfoUtil(page, pageSize).invoke();

        page = pageInfo.getPage();

        pageSize = pageInfo.getPageSize();

        Subject subject = (Subject) token.getPrincipal();

        long reqUserId = subject.getId();
        List<WhitePacketDynamicInfoVo> list = whitePacketDynamicInfoService.getDynamicInfoListByUserId(appKey,
                reqUserId, userId,page, pageSize);

        return list;
    }

    /**
     * 动态详情
     * @param token
     * @param dynamicId
     * @return
     */
    @GetMapping("/detail/{dynamicId}")
    public Object getDynamicInfo(Authentication token,@PathVariable("dynamicId") long dynamicId){
        Subject subject = (Subject) token.getPrincipal();
        return whitePacketDynamicInfoService.getDynamicInfoById(dynamicId,subject.getId());
    }


    /**
     * 赠送礼物
     */
    @PostMapping("/gift")
    public void saveGiftSpend(Authentication token, @Validated(GiftSpendCheck.class) DynamicGiftInfoDTO dynamicGiftInfoDTO) {

        Subject subject = (Subject) token.getPrincipal();

        WhitePacketDynamicDetailInfoVo dynamicDetailInfoVo = whitePacketDynamicInfoService
                .getDynamicInfoByDynamicId(dynamicGiftInfoDTO.getDynamicId());

        if(dynamicDetailInfoVo!=null){

            GiftSpend entity = getGiftSpend(dynamicGiftInfoDTO, subject, dynamicDetailInfoVo);

            whitePacketDynamicInfoService.saveDynamicGiftInfo(entity,dynamicGiftInfoDTO.getDynamicId());
        }

    }


    @GetMapping("/follow/{appKey}/{page}-{pageSize}")
    public List<WhitePacketDynamicInfoVo> getFollowDynamicInfoList(Authentication token,
                                                        @PathVariable("appKey") String appKey,
                                                        @PathVariable("page") Integer page,
                                                        @PathVariable("pageSize") Integer pageSize) {

        Subject subject = (Subject) token.getPrincipal();

        PageInfoUtil pageInfo = new PageInfoUtil(page, pageSize).invoke();

        page = pageInfo.getPage();

        pageSize = pageInfo.getPageSize();

        List<WhitePacketDynamicInfoVo> list = whitePacketDynamicInfoService
                .getFollowDynamicInfoListByUserId(appKey,subject.getId(), page, pageSize);

        return list;
    }

    private GiftSpend getGiftSpend(@Validated(GiftSpendCheck.class) DynamicGiftInfoDTO dynamicGiftInfoDTO, Subject subject, WhitePacketDynamicDetailInfoVo dynamicDetailInfoVo) {

        dynamicGiftInfoDTO.setInUserId(Integer.parseInt(dynamicDetailInfoVo.getUserId()+""));

        dynamicGiftInfoDTO.setOutUserId(Integer.parseInt(subject.getId()+""));

        GiftSpend entity = new GiftSpend();

        BeanUtils.copyProperties(dynamicGiftInfoDTO,entity);

        giftSpendService.saveGiftSpend(entity);
        return entity;
    }


}
